#include "exchange_dwg.h"
#include <memory>
#include <vector>
#include "common/system_log.h"
#include "utils/file_util.h"
#include "utils/string_util.h"

#ifdef AMCAX_DWG_ENABLE
#include "dwg_shape.h"
#include "dwg_io.h"
#endif
#include "ExchangeTopoShape.h"

AMCAX_EXCHANGE_NAMESPACE_BEGIN

void ExchangeDwg::ReadFromFilePath() 
{
#ifdef AMCAX_DWG_ENABLE
    if (!AMCAX::Exchange::file_util::is_file_exist(filePath_)) {
        LOG_ERROR("file not exist:{}",filePath_) ;
        return ;
    }
    AMCAX_EXCHANGE::DWG::DwgIo  dwg_io(filePath_) ;
    
    dwg_io.read() ;
    

    std::vector<std::shared_ptr<AMCAX_EXCHANGE::DWG::DwgShape>> dwg_shapes = dwg_io.get_shapes();
    for (auto dwg_shape_item: dwg_shapes) {
        if (dwg_shape_item->shape.IsNull()) {
            continue;
        }
        std::shared_ptr<ExchangeObject> exshape(new ExchangeObject(string_util::generate_UUID()));
        exshape->shape = std::make_shared<ExchangeTopoShape>(dwg_shape_item->shape);
        exshape->name = dwg_shape_item->name;
        exchangeObjectList_.push_back(exshape);
    }
#endif
}


void ExchangeDwg::ReadFromFileStream()
{
    throw std::runtime_error("Not implemented");
}


void ExchangeDwg::Write(const std::vector<std::shared_ptr<ExchangeObject>>& exchange_shapes, const std::shared_ptr<ExchangeDocumentInstance> &exchangeDocumentInstance)
{
    throw std::runtime_error("Not implemented");
}

AMCAX_EXCHANGE_NAMESPACE_END